package com.example.idachuappone.index.item;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.dialog.DialogDateHours;
import com.example.idachuappone.index.dialog.DialogDateHours.CallBack;
import com.example.idachuappone.index.entity.DateTime;
import com.example.idachuappone.person.item.LoginActivity;
import com.example.idachuappone.promptly.entity.Addresses;
import com.example.idachuappone.promptly.item.CookAddressListActivity;
import com.example.idachuappone.utils.CusProSmallBgDialog;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.PrefUtil;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class FamilyFeastSubmitActivity extends BaseActivity implements OnCheckedChangeListener, OnClickListener {
	private int addressRequestCode;
	private Addresses addresses;
	private Button btn_back;
	private Button btn_submit;
	private CusProSmallBgDialog cusProDialog;
	private String detailTimeFormat;
	private Dialog dialogTime;
	private EditText et_desc;
	private EditText et_number;
	private int index_address;
	private int ingredient;
	private LinearLayout ll_address;
	private LinearLayout ll_address_gone;
	private LinearLayout ll_address_visible;
	private LinearLayout ll_number;
	private LinearLayout ll_number_gone;
	private LinearLayout ll_time;
	private LinearLayout ll_time_gone;
	private LinearLayout ll_time_visible;
	private int people_num;
	private RadioGroup rg_iscutlery;
	private TextView tv_day;
	private TextView tv_desc;
	private TextView tv_hour_min;
	private TextView tv_user_address;
	private TextView tv_user_name;
	private TextView tv_user_phone;
	private TextView tv_year_mon_day;

	public FamilyFeastSubmitActivity() {
		this.ingredient = 0;
		this.addressRequestCode = 255;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_family_feast_submit);
		this.cusProDialog = new CusProSmallBgDialog(this);
		initDateTime(this);
		initView();
	}

	private void initView() {
		this.btn_submit = (Button) findViewById(R.id.btn_submit);
		this.btn_submit.setOnClickListener(this);
		this.rg_iscutlery = (RadioGroup) findViewById(R.id.rg_isfood);
		this.rg_iscutlery.setOnCheckedChangeListener(this);
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(this);
		this.ll_time = (LinearLayout) findViewById(R.id.ll_time);
		this.ll_time.setOnClickListener(this);
		this.ll_time_gone = (LinearLayout) findViewById(R.id.ll_time_gone);
		this.ll_time_visible = (LinearLayout) findViewById(R.id.ll_time_visible);
		this.tv_day = (TextView) findViewById(R.id.tv_day);
		this.tv_year_mon_day = (TextView) findViewById(R.id.tv_year_mon_day);
		this.tv_hour_min = (TextView) findViewById(R.id.tv_hour_min);
		this.ll_address = (LinearLayout) findViewById(R.id.ll_address);
		this.ll_address.setOnClickListener(this);
		this.ll_address_gone = (LinearLayout) findViewById(R.id.ll_address_gone);
		this.ll_address_visible = (LinearLayout) findViewById(R.id.ll_address_visible);
		this.tv_user_name = (TextView) findViewById(R.id.tv_user_name);
		this.tv_user_phone = (TextView) findViewById(R.id.tv_user_phone);
		this.tv_user_address = (TextView) findViewById(R.id.tv_user_address);
		initAddress();
		this.ll_number = (LinearLayout) findViewById(R.id.ll_number);
		this.ll_number.setOnClickListener(this);
		this.ll_number_gone = (LinearLayout) findViewById(R.id.ll_number_gone);
		this.et_number = (EditText) findViewById(R.id.et_number);
		this.et_number.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					FamilyFeastSubmitActivity.this.ll_number_gone
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				} else {
					FamilyFeastSubmitActivity.this.ll_number_gone.setVisibility(0);
				}
			}
		});
		this.tv_desc = (TextView) findViewById(R.id.tv_desc);
		this.et_desc = (EditText) findViewById(R.id.et_desc);
		this.et_desc.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					FamilyFeastSubmitActivity.this.tv_desc
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				} else {
					FamilyFeastSubmitActivity.this.tv_desc.setVisibility(0);
				}
			}
		});
	}

	private void initAddress() {
		PrefUtil pp = PrefUtil.getInstance(this);
		if (pp.getXiaoQu() == null || pp.getXiaoQu().length() <= 0) {
			this.ll_address_gone.setVisibility(0);
			this.ll_address_visible.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			return;
		}
		this.ll_address_gone.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		this.ll_address_visible.setVisibility(0);
		this.addresses = new Addresses("", "", pp.getName(), pp.getPhone(), pp.getArea(), pp.getXiaoQu(),
				pp.getDoor(), "", "");
		setAddress(this.addresses);
	}

	private void setAddress(Addresses addresses) {
		this.tv_user_name.setText(addresses.getName());
		this.tv_user_phone.setText(addresses.getPhone());
		this.tv_user_address.setText(addresses.getCommunity() + addresses.getDoor_number());
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		switch (checkedId) {
		case R.id.rbtn_false /* 2131165279 */:
			this.ingredient = 1;
		case R.id.rbtn_true /* 2131165280 */:
			this.ingredient = 0;
		default:
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			finish();
		case R.id.ll_address /* 2131165259 */:
			if (PrefUtil.getInstance(this).getUID() == null) {
				MainToast.show(this, "\u60a8\u8fd8\u6ca1\u6709\u767b\u5f55\uff0c\u8bf7\u5148\u767b\u5f55\u3002\u3002",
						0);
				startActivityForResult(new Intent(this, LoginActivity.class), 22);
				return;
			}
			Intent intent = new Intent(this, CookAddressListActivity.class);
			intent.putExtra("index_address", this.index_address);
			startActivityForResult(intent, this.addressRequestCode);
		case R.id.ll_time /* 2131165265 */:
			this.dialogTime.show();
		default:
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == this.addressRequestCode && resultCode == -1) {
			this.addresses = (Addresses) data.getExtras().getSerializable("address");
			setAddress(this.addresses);
			this.index_address = data.getExtras().getInt("index_address");
		}
	}

	private void initDateTime(Context context) {
		this.dialogTime = new DialogDateHours(context).showDateHours(this, new CallBack() {
			@Override
			public void btnOk(DateTime dateTime, int hourindex, String min) {
				String date = dateTime.getYear() + " " + (dateTime.getList().get(hourindex)) + ":" + min;
				Log.e("aaaaaaa", date);
				FamilyFeastSubmitActivity.this.detailTimeFormat = date;
				FamilyFeastSubmitActivity.this.ll_time_gone
						.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				FamilyFeastSubmitActivity.this.ll_time_visible.setVisibility(0);
				if (dateTime.getDay() == null || dateTime.getDay().length() <= 0) {
					FamilyFeastSubmitActivity.this.tv_day
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				} else {
					FamilyFeastSubmitActivity.this.tv_day.setVisibility(0);
					FamilyFeastSubmitActivity.this.tv_day.setText(dateTime.getDay());
				}
				FamilyFeastSubmitActivity.this.tv_year_mon_day.setText(dateTime.getYear());
				FamilyFeastSubmitActivity.this.tv_hour_min.setText(new StringBuilder(String.valueOf(dateTime.getList()
						.get(hourindex))).append(":").append(min).toString());
				FamilyFeastSubmitActivity.this.dialogTime.dismiss();
			}
		});
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (ev.getAction() == 0) {
			View v = getCurrentFocus();
			if (isShouldHideInput(v, ev)) {
				hideSoftInput(v.getWindowToken());
			}
		}
		return super.dispatchTouchEvent(ev);
	}

	private boolean isShouldHideInput(View v, MotionEvent event) {
		if (v == null || !(v instanceof EditText)) {
			return false;
		}
		int[] l = new int[2];
		v.getLocationInWindow(l);
		int left = l[0];
		int top = l[1];
		return event.getX() <= (left) || event.getX() >= (left + v.getWidth()) || event.getY() <= (top)
				|| event.getY() >= (top + v.getHeight());
	}

	private void hideSoftInput(IBinder token) {
		if (token != null) {
			((InputMethodManager) getSystemService("input_method")).hideSoftInputFromWindow(token,
					BitmapCacheManagementTask.MESSAGE_FLUSH);
			this.ll_number.setFocusable(true);
			this.ll_number.setFocusableInTouchMode(true);
		}
	}
}
